package com.haochang.chunkvod.view.vod.partition;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.ScreenUtils;
import com.haochang.chunkvod.model.order.PartInfo;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

/**
 *@author qintao
 *created at 2016/6/4 15:52
 * 分区控件
 */

public class PartLayout extends LinearLayout
{
	private Context context;
	private List<PartView> partViews = new ArrayList<PartView>();
	private int currentSelectedPos = 0;
	private OnTabChangedListener changedListener;
	private int layoutWidth = 60;

	/**
	 * 控件的宽度
	 */
	private int myViewWidth = 0;
	/**
	 * 可见的view个数
	 */
	private int visibleCount = 0;

	public PartLayout(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		this.context = context;
		initAttrs(context, attrs);
	}

	public PartLayout(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		this.context = context;
		initAttrs(context, attrs);
	}

	public PartLayout(Context context)
	{
		super(context);
		this.context = context;
	}

	private void initAttrs(Context context, AttributeSet attrs)
	{
		TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PartView, 0, 0);
		visibleCount = typeArray.getInt(R.styleable.PartView_visibleCount, 0);
		myViewWidth = ScreenUtils.getScreenWidth(context);
	}

	public int getTabWidth()
	{
		return DipPxConversion.dipToPx(context, layoutWidth);
	}

	public void addTab(PartInfo partInfo)
	{
		int total = this.getChildCount();
		String partId = partInfo.getPart_id();
		LayoutParams params = new LayoutParams(getTabWidth(), LayoutParams.WRAP_CONTENT);
		PartView partView = new PartView(context);
		partView.setLayoutParams(params);
		partView.setTag(R.id.part_value_id, partId);
		partView.setTag(total);
		partView.setText(partInfo.getPartName());
		this.addView(partView);
		partView.setOnClickListener(clickListener);
		this.partViews.add(partView);
		if (total == 0)
			partView.setSelected(true);
		else
			partView.setSelected(false);
	}

	public void setOnTabChangedListener(OnTabChangedListener changedListener)
	{
		this.changedListener = changedListener;
	}

	private OnBaseClickListener clickListener = new OnBaseClickListener()
	{

		@Override
		public void onBaseClick(View v)
		{
			int pos = (Integer) v.getTag();
			String partId = (String) v.getTag(R.id.part_value_id);
			setSelectedTable(pos, partId);
		}
	};

	public boolean setSelectedTable(int position, String partId)
	{
		if (currentSelectedPos != position && null != this.partViews && this.partViews.size() > position)
		{
			int oldPos = currentSelectedPos;
			currentSelectedPos = position;

			for (int i = 0; i < this.partViews.size(); i++)
			{
				if (i == position)
					this.partViews.get(i).setSelected(true);
				else
					this.partViews.get(i).setSelected(false);
			}

			if (changedListener != null)
			{
				changedListener.onTabChanged(oldPos, currentSelectedPos, partId);
			}
			return true;
		}
		return false;
	}

	/**
	 * 获取当前的tab position
	 * @return
	 */
	public int getCurrentTab()
	{
		return currentSelectedPos;
	}

	/**
	 * 清除tabview的数据
	 */
	public void cleanTabView()
	{
		if (partViews == null)
		{
			partViews = new ArrayList<PartView>();
		}
		if (!CollectionUtils.isEmpty(partViews))
		{
			partViews.clear();
		}
	}

	public interface OnTabChangedListener
	{
		void onTabChanged(int last, int current, String partId);
	}

}
